package com.ithiema.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ithiema.reggie.common.CustomException;
import com.ithiema.reggie.dao.DishDao;
import com.ithiema.reggie.dto.DishDto;
import com.ithiema.reggie.pojo.Dish;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithiema.reggie.pojo.DishFlavor;
import com.ithiema.reggie.service.DishFlavorService;
import com.ithiema.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishDao,Dish>implements DishService {

    public DishDto getByIdWithFlavor(Long id){
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }
    @Autowired
    private DishFlavorService dishFlavorService;
    @Transactional
    public void saveWithFlavor(DishDto dishDto){
        //保存菜品基本信息到dish
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //
        dishFlavorService.saveBatch(flavors);

    }
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto){
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }
    @Override
    @Transactional
    public void removeWithFlavors(List<Long> ids) {
        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(Dish::getId,ids);
        wrapper1.eq(Dish::getStatus,1);
        int count = this.count(wrapper1);
        if(count>0){
            throw new CustomException("该菜品在售，无法删除！");
        }
        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(wrapper2);

    }

    }


